﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EntityModule
{
    public class NewsService
    {
        public SportDataContext Service { get { return EntityService.Instance(); } }

        public void Insert(New news)
        {
            news.ID = Guid.NewGuid();
            Service.News.InsertOnSubmit(news);
            Service.SubmitChanges();
        }
        public List<New> GetNews(int page, int pageSize)
        {
            List<New> news = null;
            try
            {
                int pageCount = Service.News.Count(s => s.IsDel == null);
                if (page > 1)
                    news = Service.News.Where(s => s.IsDel == null).OrderByDescending(s => s.DateModifier).Skip(page * pageSize).Take(pageSize).ToList();
                else news = Service.News.Where(s => s.IsDel == null).OrderByDescending(s => s.DateModifier).Take(pageSize).ToList();
                if (news.Count > 0)
                    news[0].PageCount = pageCount;
            }
            catch
            {
            }
            return news;
        }

        public List<New> GetNews(Guid guid)
        {
            return Service.News.Where(s => s.ID == guid).ToList();
        }
    }
}
